package com.zhuss.product.line;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.unit.product.HotLineResp;
import com.zhuss.bean.unit.product.LineStrategySiteResp;
import com.zhuss.bean.unit.product.StrategyRequest;
import com.zhuss.bean.unit.product.TicketRequest;
import com.zhuss.bean.vo.product.line.DetailLineVo;
import com.zhuss.bean.vo.product.site.DetailSiteVo;
import com.zhuss.bean.vo.product.train.DetailLineStrategyVo;
import com.zhuss.bean.vo.product.train.ListLineStrategySiteVo;
import com.zhuss.bean.vo.product.train.ListLineStrategyVo;
import com.zhuss.common.PageVo;
import com.zhuss.enums.CanSellType;
import com.zhuss.enums.OperationStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.TrainDao;
import com.zhuss.product.db.entity.Train;
import com.zhuss.product.processor.cache.LineCache;
import com.zhuss.product.processor.cache.LineStrategyCache;
import com.zhuss.product.processor.cache.SiteCache;
import com.zhuss.product.processor.cache.SiteStrategyCache;
import com.zhuss.product.processor.cache.StrategySiteCache;
import com.zhuss.util.DateUtil;
import com.zhuss.util.PageUtils;

/**
 * @author summer
 * @date 2021/1/6 18:10
 */

@Component("productReadService")
public class ProductReadService {

    @Autowired
    @Qualifier("siteStrategyCache")
    private SiteStrategyCache siteStrategyCache;

    @Autowired
    @Qualifier("strategySiteCache")
    private StrategySiteCache strategySiteCache;

    @Autowired
    @Qualifier("lineCache")
    private LineCache lineCache;

    @Autowired
    @Qualifier("siteCache")
    private SiteCache siteCache;

    @Autowired
    @Qualifier("lineStrategyCache")
    private LineStrategyCache lineStrategyCache;

    @Autowired
    private TrainDao trainDao;


    /**
     * 根据上下车站点、线路类型查询策略
     *
     * @param req
     * @return
     */
    public Object queryTicket(TicketRequest req) throws DzcxException {
        List<ListLineStrategyVo> strategyList = new ArrayList<>();
        if ((req.getLineTypeList() != null && req.getLineTypeList().size() >0)
                && (req.getStartPoint() == null && req.getStartPoint() == null)) {
            strategyList =  siteStrategyCache.getType(req.getLineTypeList().get(0));
        }
        if ((req.getStartPoint() != null && req.getEndPoint() != null)) {
            strategyList = siteStrategyCache.get(req.getStartPoint(),req.getEndPoint(),req.getLineTypeList());
        }

        if ((req.getLineTypeList() != null && req.getLineTypeList().size() >0)
                && (req.getStartPoint() != null && req.getEndPoint() == null)) {
            strategyList = siteStrategyCache.get(req.getStartPoint(),req.getLineTypeList());
        }
        PageVo<ListLineStrategyVo> result = siteHandler(strategyList,req);
        return result;
    }

    /**
     * 根据线路类型查询策略
     *
     * @param req
     * @return
     */
    public Object queryTicketByLineType(TicketRequest req) throws DzcxException {
        if (req.getLineType() == null || req.getLineType() <= 0l) {
            throw new DzcxException(ExceptionCode.线路类型为空);
        }
        List<ListLineStrategyVo> strategyList = siteStrategyCache.getType(req.getLineType());
        PageVo<ListLineStrategyVo> result = siteHandler(strategyList,req);
        return result;
    }

    /**
     * 根据策略id查询站点信息
     *
     * @param request
     * @throws DzcxException
     */
    public Object querySiteInfo(StrategyRequest request) throws DzcxException {
        if (request.getLineId() == null || request.getLineId() == 0) {
            throw new DzcxException(ExceptionCode.线路ID为空);
        }
        if (request.getLineStrategyId() == null || request.getLineStrategyId() <= 0l) {
            throw new DzcxException(ExceptionCode.车次id为空);
        }
        LineStrategySiteResp resp = new LineStrategySiteResp();
        DetailLineStrategyVo strategyVo = lineStrategyCache.get(request.getLineStrategyId());
        List<ListLineStrategySiteVo> siteList = strategySiteCache.get(request.getLineStrategyId());
        resp.setStrategySiteVos(siteList);
        Long startPrice = 0l;
        Long endPrice = 0l;
        Integer arriveTime = 0;
        for (ListLineStrategySiteVo site : siteList) {
            DetailSiteVo siteVo = siteCache.get(site.getSiteId());
            site.setSiteName(siteVo.getName());
            site.setLng(siteVo.getLng());
            site.setLat(siteVo.getLat());
            if (site.getSiteId().equals(request.getStart())) {
                startPrice = site.getPrice();
            }
            if (site.getSiteId().equals(request.getEnd())) {
                endPrice = site.getPrice();
            }
            arriveTime = arriveTime + site.getNearestDuration();
            site.setArriveTime(DateUtil.minTime(arriveTime,strategyVo.getStartTime()));
        }
        resp.setPrice((endPrice - startPrice) ==0l ? 100 : (endPrice - startPrice));
        return resp;
    }


    /**
     * 根据策略id请选择出发日期
     * 查询今天之后的车次信息
     *
     * @param
     * @throws DzcxException
     */
    public Object queryTrainDate(Long lineStrategyId) throws DzcxException {
        if (lineStrategyId == null || lineStrategyId <= 0l) {
            throw new DzcxException(ExceptionCode.车次id为空);
        }
        Date now = new Date();
        now = DateUtil.toDateOnly(now);
        List<Train> trainList = trainDao.listTrainByLineStrategyId(lineStrategyId,OperationStatus.已开通.key(),CanSellType.售卖.key(), now);
        return trainList;
    }


    /**
     * 查询热门线路
     * @return
     * @throws DzcxException
     */
    public Object queryHotLine() throws DzcxException {
        List<HotLineResp> hotLineList = trainDao.queryHotLine(CanSellType.售卖.key(), OperationStatus.已开通.key());
        for (HotLineResp resp : hotLineList) {
            DetailLineStrategyVo vo =  lineStrategyCache.get(resp.getLineStrategyId());
            resp.setBoardNo(vo.getBoardNo());
            resp.setStrategyType(vo.getStrategyType());
            List<ListLineStrategySiteVo> siteVoList = strategySiteCache.get(resp.getLineStrategyId());
            resp.setOnSiteId(siteVoList.get(0).getSiteId());
            resp.setOffSiteId(siteVoList.get(siteVoList.size() -1).getSiteId());
        }
        return hotLineList;
    }


    //站点处理
    public PageVo<ListLineStrategyVo> siteHandler(List<ListLineStrategyVo> strategyList, TicketRequest req) throws DzcxException {
        List<ListLineStrategyVo> pageList = PageUtils.startPage(strategyList, req.getPageNum(),req.getPageSize());
        for (ListLineStrategyVo vo : pageList) {
            DetailLineVo line = lineCache.get(vo.getLineId());
            vo.setLineName(line.getName());
            vo.setRideType(line.getRideType());
            vo.setStartPointName(line.getStartPointName());
            vo.setEndPointName(line.getEndPointName());
            List<ListLineStrategySiteVo> siteVoList = strategySiteCache.get(vo.getLineStrategyId());
            vo.setOnSiteId(siteVoList.get(0).getSiteId());
            vo.setOffSiteId(siteVoList.get(siteVoList.size() -1).getSiteId());
            //获取到达终点时间
            Integer time = line.getIntervals();
            vo.setEndTime(time == null ? vo.getStartTime() : DateUtil.minTime(time,vo.getStartTime()));
        }

        PageVo<ListLineStrategyVo> result = new PageVo<ListLineStrategyVo>();
        result.setPageNum(req.getPageNum());
        result.setPageSize(req.getPageSize());
        result.setItems(pageList);
        result.setTotal(strategyList.size());
        return result;
    }
}
